package com.example.experiment4.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.experiment4.dao.PersonDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WriteToExcel {

    @Resource
    PersonDao dao;

    public void write() {
        List<String> row1 = CollUtil.newArrayList("姓名条目数", "电话条目数", "电话和地址条目数");
        int countName = dao.countPersonByName();
        int countAddress = dao.countPersonByPhoneAndAddress();
        int countPhone = dao.countPersonByPhone();
        List<String> row2 = CollUtil.newArrayList(String.valueOf(countName), String.valueOf(countPhone), String.valueOf(countAddress));
        List<List<String>> rows = CollUtil.newArrayList(row1, row2);

        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/writeTest1.xlsx");
        //通过构造方法创建writer
        //ExcelWriter writer = new ExcelWriter("d:/writeTest.xls");

        //跳过当前行，既第一行，非必须，在此演示用
        writer.passCurrentRow();

        //合并单元格后的标题行，使用默认标题样式
        writer.merge(row1.size() - 1, "测试结果");
        //一次性写出内容，强制输出标题
        writer.write(rows, true);
        //关闭writer，释放内存
        writer.close();

    }
}
